package cs235.sort;

public class SelectionSort extends TannersSort implements Sort {
    
    /**
     * selection sort.
     * 
     * @param a
     *            an array of Comparable items.
     */
    public static void selectionSort(Comparable[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < a.length; j++)
                if (a[j].compareTo(a[min]) < 0)
                    min = j;

            Comparable tmp = a[min];
            a[min] = a[i];
            a[i] = tmp;
        }
    }

    @Override
    public void sort(Comparable[] a) {
        compares = 0;
        for (int i = 0; i < a.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < a.length; j++){
                ++compares;
                if (a[j].compareTo(a[min]) < 0){
                    min = j;
                }
            }
            Comparable tmp = a[min];
            a[min] = a[i];
            a[i] = tmp;
        }
    }
}
